using System;
using HyperActive.Core;
using HyperActive.SchemaProber;

namespace SimplePMT.Components.ActiveRecordExtensions
{
	public class SimplePMTNameProvider : NameProvider
	{
		/// <summary>
		/// If the name ends with lowercase character followed by 'ID' in upper case, remove ID
		/// </summary>
		/// <param name="foreignKeyName">Name of the foreign key.</param>
		/// <returns></returns>
		public string CleanForeignKeyName(string foreignKeyName)
		{
			string result = foreignKeyName;
			if (result.Length > 3)
			{
				//get the last 3 chars
				string lastthree = result.Substring(result.Length - 3);
				if (Char.IsLower(lastthree[0]) && lastthree.Substring(1) == "ID")
					result = result.Substring(0, result.Length - 2);
			}
			return result;
		}
		public override string GetFieldName(ForeignKeyColumnSchema foreignkey)
		{
			string fkname = CleanForeignKeyName(foreignkey.Name);
			return base.GetFieldName(fkname);
		}
		public override string GetPropertyName(ForeignKeyColumnSchema foreignkey)
		{
			string fkname = CleanForeignKeyName(foreignkey.Name);
			return base.GetPropertyName(fkname);
		}
	}
}
